<?php
namespace app\api\controller\v1;
use GuzzleHttp\Client;
use think\Controller;
use app\common\lib\Curl;
class Common extends Controller
{
	protected $access_token;
	
	public function __construct(){
		
		parent::__construct();
		
	}
	//根据设备code获取设备id
	public function getDeviceId($device_code){
        $map['access_token'] = config('app.access_token');
        $map['key'] =$device_code;
		$c = (new Curl())->post("https://mp.uxuj.cn/admin/api/device/getDeviceKeyId",$map);
		$d = json_decode($c,true);
		return $d['data']['id'];
	}
    //检查设备是否激活
	public function get_device_code($data){
        $map['access_token'] = config('app.access_token');
        $map['mac'] =$data['mac'];
        $map['sn'] =$data['device_sn'];
        $map['brand'] =$data['brand_name'];
        $map['model'] =$data['model'];
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/device/check",$map);
        $d = json_decode($c,true);
        return $d['data'];

    }
    //设备激活
    public function device_active($data){
	    $map['access_token'] = config('app.access_token');
	    $map['mac'] =$data['mac'];
	    $map['key'] =$data['device_code'];
	    $map['sn'] =$data['device_sn'];
	    $map['brand'] =$data['brand_name'];
	    $map['model'] =$data['model'];
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/device/active",$map);
        $d = json_decode($c,true);
        return $d;
    }
    //根据卡号获取学生信息
    public function card_info($data){
        $map['access_token'] = config('app.access_token');
        $map['card_number'] =$data['card_number'];
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/student/getByCardNumber",$map);
        $d = json_decode($c,true);
        return $d['data'];
    }
    //通过id获取当前班级的基本信息
    public function class_info_id($id){
        $map = [
            'access_token'=>config('app.access_token'),
            'class_id'=>$id
        ];
        // p($map);die;
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/classes/profile",$map);
        $d = json_decode($c,true);
        return $d['data'];
    }
    //app更新
    public function app_version(){
        $c = (new Curl())->get("http://admi.keji110.com/plate/version");
        $d = json_decode($c,true);
        return $d;
    }
    //通过id获取班级人脸库 教师、学生
    public function student_faces($class_id){
        $map = [
            'access_token'=>config('app.access_token'),
            'id'=>$class_id
        ];
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/classes/faces",$map);
        $d = json_decode($c,true);
        return $d['data'];
    }
    //根据学生ids获取学生信息
    public function get_student_info($ids){
        $map = [
            'access_token'=>config('app.access_token'),
            'ids'=>$ids
        ];
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/student/profile",$map);
        $d = json_decode($c,true);
        return $d['data'];
    }
    //获取周课程表
    public function get_timetable($class_id){
        $map = [
            'access_token'=>config('app.access_token'),
            'class_id'=>$class_id
        ];
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/classes/course",$map);
        $d = json_decode($c,true);
        return $d['data'];
    }
    //获取今日课程表
    public function get_today_timetable($class_id){
        $map = [
            'access_token'=>config('app.access_token'),
            'class_id'=>$class_id
        ];
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/classes/todayCourse",$map);
        $d = json_decode($c,true);
        return $d['data'];
    }
    //获取班级光荣榜
    public function get_honer($class_id){
        $map = [
            'id'=>$class_id
        ];
        $c = (new Curl())->post("https://suzhi.uxuj.cn/v6/classes/ranking",$map);
        $d = json_decode($c,true);
        return $d['data'];
    }
    //替换图片路径
    public function imageUrl($content) {
        $url = config('app.app_host');
        if (preg_match('/(http:\/\/)|(https:\/\/)/i', $content)) {
            $url = "";
        }else{
            $url = $url;
        }
        $pregRule = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.jpg|\.jpeg|\.png|\.gif|\.bmp]))[\'|\"].*?[\/]?>/";
        $list = preg_replace($pregRule, '<img src="' . $url . '${1}" style="max-width:100%">', $content);
        return $list;
    }
    public function get_teacher_timeable($id){

        $map = [
            'id'=>$id
        ];
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/teacher/course",$map);
        $d = json_decode($c,true);
        return $d['data'];
    }

    //给客户端发送多对多信息
    //$param 对应班牌的设备号(注册码)
    //$content 内容
    public function socketPush($class_ids, $content)
    {

        $map['access_token'] =  config('app.access_token');
        $map['content'] = json_encode($content);
        $device_ids = db('device_association')->where('class_id', 'in', $class_ids)->column('device_id');
        if (is_array($device_ids) && count($device_ids, COUNT_NORMAL) > 0) {
            $map['device_ids'] = $device_ids;
            $client = new Client([
                'base_uri' => 'https://mp.uxuj.cn/admin/',
                'timeout' => 30,
                'allow_redirects' => true
            ]);
            $response = $client->request('POST', 'api/socket/publish', [
                'form_params' => $map
            ]);
            $body = $response->getBody();
            $contents = [];
            if ($body->getSize()) {
                $contents = $body->getContents();
            }
            return $contents;
        } else {
            return show(0, "暂无发送客户端", 0, 200);
        }
    }
    //给客户端发送多对多信息
    //$param 对应班牌的设备号(注册码)
    //$content 内容
    public function socketPush_device($device_ids, $content)
    {

        $map['access_token'] = config('app.access_token');
        $map['content'] = json_encode($content);
        if (is_array($device_ids) && count($device_ids, COUNT_NORMAL) > 0) {
            $map['device_ids'] = $device_ids;
            $client = new Client([
                'base_uri' => 'https://mp.uxuj.cn/admin/',
                'timeout' => 30,
                'allow_redirects' => true
            ]);
            $response = $client->request('POST', 'api/socket/publish', [
                'form_params' => $map
            ]);
            $body = $response->getBody();
            $contents = [];
            if ($body->getSize()) {
                $contents = $body->getContents();
            }
            return $contents;
        } else {
            return show(0, "暂无发送客户端", 0, 200);
        }
    }
    //通过班级id换取学校老师列表
    public function codeToTeacherLists($class_id){
        $map = [
            'access_token'=>config('app.access_token'),
            'id'=>$class_id
        ];
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/classes/schoolTeachers",$map);
        $d = json_decode($c,true);
        return $d['data'];
    }
    //通过班级id获取班级学生列表
    public function students_list($class_id){
        $map = [
            'access_token' => config('app.access_token'),
            'id' => $class_id
        ];
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/classes/students", $map);
        $d = json_decode($c, true);
        return $d['data'];
    }
    //根据卡号获取学生信息
    public function get_by_card_student($card_number,$class_id){
        $map = [
            'access_token' => config('app.access_token'),
            'card_number' => $card_number,
            'class_id'=>$class_id
        ];
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/student/getByCardNumber", $map);
        $d = json_decode($c, true);
        return $d['data'];
    }
    //根据卡号获取老师信息
    public function get_by_card_teacher($card_number,$class_id){
        $map = [
            'access_token' => config('app.access_token'),
            'card_number' => $card_number,
            'class_id' => $class_id
        ];
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/teacher/getByCardNumber", $map);
        $d = json_decode($c, true);
        return $d['data'];
    }
    //根据卡号获取老师信息
    public function get_by_card_info($card_number,$class_id){
        $map = [
            'access_token' => config('app.access_token'),
            'card_number' => $card_number,
            'class_id' => $class_id
        ];
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/card/getByNumber", $map);
        $d = json_decode($c, true);
        return $d['data'];
    }
    //检测该班级的课程是否需要考勤
    public function is_check_attendace($class_id){

        $map = [
            'access_token' => config('app.access_token'),
            'id' => $class_id
        ];
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/classes/currentCourseAttendance", $map);
        $d = json_decode($c, true);
        return $d;
    }
    public function get_curriculum_info($event_id,$student_id){
        $map = [
            'access_token' => config('app.access_token'),
            'course_id' => $event_id,
            'student_id'=>$student_id
        ];
        $c = (new Curl())->post("https://mp.uxuj.cn/admin/api/course", $map);
        $d = json_decode($c, true);
        return $d['data'];
    }
    //给客户端发送多对多信息
    //$param 对应班牌的设备号(注册码)
    //$content 内容
    public function socket_push($class_ids, $content)
    {

        $map['access_token'] =  config('app.access_token');
        $map['content'] = json_encode($content);
        $device_ids = db('device_association')->where('class_id', 'in', $class_ids)->column('device_id');
        if (is_array($device_ids) && count($device_ids, COUNT_NORMAL) > 0) {
            $map['device_ids'] = $device_ids;
            $client = new Client([
                'base_uri' => 'https://mp.uxuj.cn/admin/',
                'timeout' => 30,
                'allow_redirects' => true
            ]);
            $response = $client->request('POST', 'api/socket/publish', [
                'form_params' => $map
            ]);
            $body = $response->getBody();
            $contents = [];
            if ($body->getSize()) {
                $contents = $body->getContents();
            }
            return $contents;
        } else {
            return show(0, "暂无发送客户端", 0, 200);
        }
    }
}